' VB.NET helper that uses Word to convert a stream of text or rtf to html
Imports System.IO.Path
Imports Word = Microsoft.Office.Interop.Word

Public Class WordAutomationWrapper

    Dim app As Word.Application
    Dim doc As Word.Document
    'Changed the filename type from string to object(Conversion Manual)
    Dim filename As Object = Combine(GetTempPath, GetTempFileName)



    ' Initialize the Word application and create a new document
    Public Sub Initialize()

        app = CreateObject("Word.Application")
        doc = app.Documents.Add()

    End Sub

    ' Convert the input text to html
    Public Function ConvertToHtml(ByVal text As String) As String

        doc.Activate()
        app.Selection.TypeText(text)
        doc.SaveAs(filename, Word.WdSaveFormat.wdFormatHTML)
        Return doc.Content().Text

    End Function

    ' Clean up: close the document, release the Com objects, nullify the Com objects, and delete the file
    Public Sub Terminate()

        doc.Close()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(doc)

        app.Quit()
        System.Runtime.InteropServices.Marshal.ReleaseComObject(app)

        doc = Nothing
        app = Nothing

        System.IO.File.Delete(filename)

    End Sub
    'Changed the mainWordDoc,compareDoc,outputDoc types from string to object(Conversion Manual)
    Public Sub CompareWordDocs(ByVal mainWordDoc As Object, ByVal compareDoc As Object, ByVal outputDoc As Object)

        app.Visible = True

        Try
            Dim mainDoc As Word.Document = app.Documents.Open(mainWordDoc)
            '    strOutFile = CreateTempFile("DWWC") & ".doc"

            mainDoc.Compare(compareDoc, , 2, False, True)

            Dim newDoc As Word.Document = app.ActiveDocument()

            newDoc.SaveAs(outputDoc, Word.WdSaveFormat.wdFormatRTF)
            newDoc.TrackRevisions = True
            newDoc.ShowRevisions = True
            'newDoc.Compare(compareDoc, , 2)
            newDoc.Save()

            newDoc.Close()
            mainDoc.Close()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mainDoc)
            System.Runtime.InteropServices.Marshal.ReleaseComObject(newDoc)


            mainDoc = Nothing
            newDoc = Nothing

        Catch ex As Exception
            System.Diagnostics.Trace.WriteLine(ex.Message)
            Throw ex
        Finally
            app.Quit()
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app)
            app = Nothing
        End Try


    End Sub

End Class
